package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ArchiveUsagePurpose;
import com.ruoyi.system.service.ArchiveUsagePurposeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 档案利用目的统计 Controller
 * 提供接口给前端调用，实现前后端交互
 *
 * @author 你的名字
 */
@RestController
@RequestMapping("/archive/usagePurpose")
public class ArchiveUsagePurposeController {

    @Autowired
    private ArchiveUsagePurposeService purposeService;

    /**
     * 查询档案利用目的统计列表（支持名称模糊查询）
     */
    @GetMapping("/list")
    public AjaxResult list(ArchiveUsagePurpose purpose) {
        List<ArchiveUsagePurpose> list = purposeService.selectArchiveUsagePurposeList(purpose);
        Set<String> names = new HashSet<>();
        if(list != null && list.size() > 0)
        {
            names = list.stream().map(m -> m.getName()).collect(Collectors.toSet());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("seriesData",list);
        map.put("xData",names);
        return AjaxResult.success(map); // 封装分页结果
    }

    /**
     * 根据主键ID查询档案利用目的统计详情
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ArchiveUsagePurpose purpose = purposeService.selectArchiveUsagePurposeById(id);
        return AjaxResult.success(purpose);
    }

    /**
     * 新增档案利用目的统计记录
     */
    @PostMapping
    public AjaxResult add(@RequestBody ArchiveUsagePurpose purpose) {
        int result = purposeService.insertArchiveUsagePurpose(purpose);
        return AjaxResult.success(result);
    }

    /**
     * 修改档案利用目的统计记录
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ArchiveUsagePurpose purpose) {
        int result = purposeService.updateArchiveUsagePurpose(purpose);
        return AjaxResult.success(result);
    }

    /**
     * 删除档案利用目的统计记录（单条）
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        int result = purposeService.deleteArchiveUsagePurposeById(id);
        return AjaxResult.success(result);
    }

    /**
     * 批量删除档案利用目的统计记录
     */
    @DeleteMapping("/batch/{ids}")
    public AjaxResult removeBatch(@PathVariable Long[] ids) {
        int result = purposeService.deleteArchiveUsagePurposeByIds(ids);
        return AjaxResult.success(result);
    }

    /**
     * 批量保存档案利用目的统计数据（先删后插）
     */
    @PostMapping("/batchSave")
    public AjaxResult batchSave(@RequestBody List<ArchiveUsagePurpose> list) {
        int result = purposeService.batchSaveArchiveUsagePurpose(list);
        return AjaxResult.success(result);
    }
}